package com.atcc.test.serviceImpl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atcc.test.service.SmsService;



@Service
public class SmsServiceImpl implements SmsService{

	@Override
	public boolean SendMessage(String PhoneNumber,Map<String, Object> code) {
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "xxx", "xxx");//阿里云服务器自己生成的账号密码
        IAcsClient client = new DefaultAcsClient(profile);
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//不能动
        request.setSysVersion("2017-05-25");//不能动
        request.setSysAction("SendSms");
        
        //自定义的参数
        request.putQueryParameter("PhoneNumbers", PhoneNumber);
        request.putQueryParameter("SignName", "热爱生活的艺术门徒");
        request.putQueryParameter("TemplateCode", "SMS_201717176");
        //构建一个短信验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
		return false;
	}

}
